<template>
  <div class="login-container">
    <div class="left" />
    <div class="right">
      <div class="login-box">
        <h2>登录</h2>
        <el-form ref="formRef" :model="formData" :rules="formRules" label-position="right" label-width="80px">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="formData.mobile" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="formData.isAgree">用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="doLogin">确认</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import { sysLoginRequest } from '@/api/user'
export default {
  name: 'LoginPage',
  data() {
    return {
      formData: {
        mobile: '13800000002',
        password: '123456',
        isAgree: false
      },
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{3,15}$/, message: '密码必须由3-15位数字字母下划线组成', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => value ? callback() : callback(new Error('请勾选用户协议')),
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 登录校验
    doLogin() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          // 一次封装写法
          /* request.post('/sys/login', {
            mobile: this.formData.mobile,
            password: this.formData.password
          }).then(res => {
            console.log(res)
          }).catch(err => {
            console.log(err)
          }) */

          // 二次网络层封装写法
          /* sysLoginRequest(this.formData).then(res => {
            console.log(res)
          }).catch(err => {
            console.log(err)
          }) */

          // async await 优化写法
          /* try {
            const res = await sysLoginRequest(this.formData)
            console.log(res)
          } catch (error) {
            //
          } */

          // 调用vuex里面的actions将token存起来
          await this.$store.dispatch('user/loginAction', this.formData)
          this.$router.push('/dashboard')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-container{
    display: flex;
    height: 100vh;
    .left{
      flex: 3;
      border-top-right-radius: 50px;
      border-bottom-right-radius: 50px;
      background: url(~@/assets/common/login_back.png) no-repeat left top/cover;
    }
    .right{
      flex: 2;
        display: flex;
        justify-content: center;
        align-items: center;
      .login-box{
        width: 80%;
        padding: 50px 30px;
        border: 1px solid #cec0c0;
        border-radius: 30px;
        box-shadow: 10px 10px 10px rgb(162, 156, 156);
        h2{
          margin-left: 20px;
        }
        .el-form{
          width: 80%;
        }
        .el-button{
          width: 100%;
        }
      }
    }
  }
</style>
